package com.hex.ds.hdrs.period.job.controller;


import com.hex.bigdata.hbda.annotation.Log;
import com.hex.bigdata.hbda.base.Page;
import com.hex.bigdata.hbda.utils.StringUtils;
import com.hex.ds.hdrs.period.job.converter.IPeriodJobVoConverter;
import com.hex.ds.hdrs.period.job.dto.PeriodJobDto;
import com.hex.ds.hdrs.period.job.po.PeriodJob;
import com.hex.ds.hdrs.period.job.service.PeriodJobService;
import com.hex.ds.hdrs.period.job.vo.PeriodJobCopyVo;
import com.hex.ds.hdrs.period.job.vo.PeriodJobVo;
import com.hex.ds.hdrs.period.job.vo.query.PeriodJobQueryVo;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Package: com.hex.ds.hdrs.period.job.controller
 * @ClassName PeriodJobController
 * @Description: 周期作业实例表 前端控制器
 * @Author: jd.chen
 * @Date 2021/7/8 16:02
 * @Version v1.0
 **/
@Slf4j
@RestController
@RequestMapping("/api/periodJob")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PeriodJobController {

    private final PeriodJobService periodJobService;

    private final IPeriodJobVoConverter converter;

    /*
     * @Method: queryListByPage <br>
     * @Param: [periodJobQueryVo, page] <br>
     * @Return: org.springframework.http.ResponseEntity<java.lang.Object> <br>
     * @Description：分页查询-周期作业实例<br>
     * @Author： wz.li<br>
     * @Date： 2023/4/28 17:55 <br>
     * @Version： V2.0.2<br>
     */
    @GetMapping
    @ApiOperation("分页查询-周期作业实例")
    public ResponseEntity<Object> queryListByPage(PeriodJobQueryVo periodJobQueryVo, Page page) {
        log.debug("【PeriodJobController.queryListByPage】参数: {}", periodJobQueryVo);
        PeriodJobDto periodJobDto = converter.toPeriodJobDto(periodJobQueryVo);
        return new ResponseEntity<>(periodJobService.queryListByPage(periodJobDto, page), HttpStatus.OK);
    }

    @PostMapping("/batchChangeBizDate")
    @Log("批量修改作业业务日期")
    @ApiOperation("批量修改作业业务日期")
    public ResponseEntity<Object> batchUpdateJobStatus(@RequestBody PeriodJobVo periodJobVo) {
        log.debug("【PeriodJobController.batchUpdateJobStatus】参数periodJobVo: {}", periodJobVo);
        this.periodJobService.batchUpdateJobBizDate(periodJobVo);
        return new ResponseEntity<>("修改成功！", HttpStatus.OK);
    }

    /**
     * 删除作业信息
     *
     * @param pkIds
     * @return
     */
    @DeleteMapping
    @Log("删除作业信息")
    @ApiOperation("删除作业信息")
    public ResponseEntity<Object> delJobInfo(@RequestBody List<String> pkIds) {
        log.debug("【PeriodJobController.delJobInfo】参数periodJob: {}", pkIds);
        this.periodJobService.delJobInfo(pkIds);
        return new ResponseEntity<>("OK!", HttpStatus.OK);
    }

    @GetMapping("/jobInfoDetail")
    @Log("查询作业详情")
    @ApiOperation("查询作业详情")
    public ResponseEntity<Object> jobInfoDetail(@RequestParam String pkId) {
        log.debug("【PeriodJobController.jobInfoDetail】参数pkId: {}", pkId);
        PeriodJob periodJob = this.periodJobService.getJobInfoByPkId(pkId);
        return new ResponseEntity<>(periodJob, HttpStatus.OK);
    }

    /*
     * @Method: copy <br>
     * @Param: [periodJobCopyVo] <br>
     * @Return: org.springframework.http.ResponseEntity<java.lang.Object> <br>
     * @Description：复制周期作业实例<br>
     * @Author： wz.li<br>
     * @Date： 2023/5/5 10:05 <br>
     * @Version： V2.0.2<br>
     */
    @Log("复制周期作业实例")
    @PutMapping("/copy")
    @ApiOperation("复制周期作业实例")
    public ResponseEntity<Object> copyPeriodJob(@RequestBody PeriodJobCopyVo periodJobCopyVo) {
        log.info("【PeriodJobController.copyPeriodJob】periodJob: {}", periodJobCopyVo.toString());
        periodJobService.copyPeriodJob(converter.toPeriodJobCopyDto(periodJobCopyVo));
        return new ResponseEntity<>("OK!", HttpStatus.OK);
    }

    @Log("追数历史作业")
    @PutMapping("/chasingData")
    @ApiOperation("追数历史作业")
    public ResponseEntity<Object> chasingData(@RequestBody PeriodJobCopyVo periodJobCopyVo) {
        log.info("【PeriodJobController.copyPeriodJob】chasingData: {}", periodJobCopyVo.toString());
        periodJobService.chasingDataPeriodJob(converter.toPeriodJobCopyDto(periodJobCopyVo));
        return new ResponseEntity<>("OK!", HttpStatus.OK);
    }

    /**
     * @Method: runAgainOne
     * @Param: [pkIds]
     * @Return: org.springframework.http.ResponseEntity<java.lang.Object>
     * @Description： 批量重跑单个作业及其下的任务
     * @Author： jd.chen
     * @Date： 2021/6/21 20:15
     * @Version： V1.0
     */
    @Log("批量重跑单个作业及其下的任务")
    @ApiOperation("批量重跑单个作业及其下的任务")
    @PostMapping(value = "/runAgainOne")
    public ResponseEntity<Object> runAgainOne(@RequestBody List<String> pkIds) {
        log.info("批量重跑周期作业【【PeriodJobController.runAgainOne】】参数pkIds: {}", StringUtils.join(pkIds, ","));
        this.periodJobService.runAgainOne(pkIds);
        return new ResponseEntity<>("OK!", HttpStatus.OK);
    }
}

